import { LotteryDrawingStatus } from '@/enums/lottery'
import type { Prize } from './Prize'
import { PrizeLevel } from '@/enums/prize'
import type { Winner } from './Winner'
import { startLottery } from '@/api'

export class NewLottery {
  private prizes: Prize[]
  private currentPrizeIndex: number
  private drawingStatus: LotteryDrawingStatus
  private drawCount: number
  private lastWinners: Winner[]
  private isAnimating: boolean
  private availableTickets: string[]

  constructor(prizes: Prize[]) {
    this.prizes = prizes
    this.currentPrizeIndex = 0
    this.drawingStatus = LotteryDrawingStatus.NOT_STARTED
    this.drawCount = 1
    this.lastWinners = []
    this.isAnimating = false
    this.availableTickets = []
  }

  // 获取当前奖项
  getCurrentPrize(): Prize {
    return this.prizes[this.currentPrizeIndex]
  }

  // 获取奖项等级名称
  getPrizeLevel(): string {
    return this.getCurrentPrize().getPrizeLevel()
  }

  // 获取奖项名称
  getPrizeName(): string {
    return this.getCurrentPrize().getName()
  }

  // 获取剩余可抽取数量
  getRemainingCount(): number {
    const prize = this.getCurrentPrize()
    return prize.getNumberOfDraws() - prize.getNumberOfWinners()
  }

  // 切换到上一个奖项
  async setPreviousPrize(): Promise<void> {
    if (this.currentPrizeIndex > 0) {
      this.currentPrizeIndex--
      this.resetDrawingState()
    }
  }

  // 切换到下一个奖项
  async setNextPrize(): Promise<void> {
    if (this.currentPrizeIndex < this.prizes.length - 1) {
      this.currentPrizeIndex++
      this.resetDrawingState()
    }
  }

  // 是否是第一个奖项
  isFirstPrize(): boolean {
    return this.currentPrizeIndex === 0
  }

  // 是否是最后一个奖项
  isLastPrize(): boolean {
    return this.currentPrizeIndex === this.prizes.length - 1
  }

  // 设置抽奖状态
  setDrawingStatus(status: LotteryDrawingStatus): void {
    this.drawingStatus = status
  }

  // 获取抽奖状态
  getDrawingStatus(): LotteryDrawingStatus {
    return this.drawingStatus
  }

  // 设置抽奖数量
  setDrawCount(count: number): void {
    if (count >= 1 && count <= this.getRemainingCount()) {
      this.drawCount = count
    }
  }

  // 获取抽奖数量
  getDrawCount(): number {
    return this.drawCount
  }

  // 增加抽奖数量
  increaseDrawCount(): void {
    if (this.drawCount < this.getRemainingCount()) {
      this.drawCount++
    }
  }

  // 减少抽奖数量
  decreaseDrawCount(): void {
    if (this.drawCount > 1) {
      this.drawCount--
    }
  }

  // 设置动画状态
  setAnimating(value: boolean): void {
    this.isAnimating = value
  }

  // 获取动画状态
  isAnimatingNow(): boolean {
    return this.isAnimating
  }

  // 设置可用号码
  setAvailableTickets(tickets: string[]): void {
    this.availableTickets = tickets
  }

  // 获取可用号码
  getAvailableTickets(): string[] {
    return this.availableTickets
  }

  // 设置最近的获奖者
  setLastWinners(winners: Winner[]): void {
    this.lastWinners = winners
  }

  // 获取最近的获奖者
  getLastWinners(): Winner[] {
    return this.lastWinners
  }

  // 重置抽奖状态
  private resetDrawingState(): void {
    this.drawingStatus = LotteryDrawingStatus.NOT_STARTED
    this.drawCount = 1
    this.lastWinners = []
    this.isAnimating = false
  }

  // 抽奖
  async draw() {
    if (this.drawingStatus !== LotteryDrawingStatus.IN_PROGRESS) {
      throw new Error('抽奖未开始')
    }

    const prize = this.getCurrentPrize()
    const { data } = await startLottery(prize.getId(), prize.getNumberOfDraws())
    if (data.code !== 0) {
      return {
        list: [],
        remaining: -1
      }
    }

    return {
      list: data.data.awardlist.map((item) => item.number),
      remaining: data.data.unawardnum
    }
  }
}
